package cn.com.cetccst.dataupload.tools;

import androidx.work.BackoffPolicy;
import androidx.work.Constraints;
import androidx.work.Data;
import androidx.work.NetworkType;
import androidx.work.OneTimeWorkRequest;
import androidx.work.OutOfQuotaPolicy;

import java.util.concurrent.TimeUnit;

public class WorkerUtil {

    public static OneTimeWorkRequest getOneTimeWorkRequest(Class clazz, Data data) {

        Constraints constraints = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build();

        OneTimeWorkRequest.Builder requestBuilder = new OneTimeWorkRequest.Builder(clazz)
                .setConstraints(constraints)
                .setBackoffCriteria(BackoffPolicy.EXPONENTIAL,
                        OneTimeWorkRequest.MIN_BACKOFF_MILLIS,
                        TimeUnit.MILLISECONDS)
                .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST);
        if (data != null) {
            requestBuilder.setInputData(data);
        }
        return requestBuilder.build();
    }
}
